<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use App\Models\Worklinkstatistic;
use App\Models\Worklink;
use App\Models\Worklinktotal;

class Worklinkstatic extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'work:linkstatistic';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '微信客服每日0点统计客服链接';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 昨天的日期
        $yesterday = date("Ymd",strtotime("-1 day"));
        // 前提的日期
        $beforeyesterday = date("Ymd",strtotime("-3 day"));
        $yesterdayPageViews = Redis::hgetall('work_page_view_'.$yesterday);

        $workLinkStatistic = [];
        $handleNumber = 1;
        foreach ($yesterdayPageViews as $kfid => $pageView) {
            $workClicks = Redis::hget('work_click_'.$yesterday,$kfid);
            $linkWelcomes = Redis::hget('work_kefu_'.$yesterday,$kfid);
            $qrcodeShows = Redis::hget('work_qrcode_show_'.$yesterday,$kfid);
            $qrcodePresss = Redis::hget('work_qrcode_press_'.$yesterday,$kfid);
            $qrcodePressCards = Redis::hget('work_qrcode_press_card_'.$yesterday,$kfid);

            $workClick = $workClicks ? $workClicks : 0;
            $linkWelcome = $linkWelcomes ? $linkWelcomes : 0;
            $qrcodeShow = $qrcodeShows ? $qrcodeShows : 0;
            $qrcodePress = $qrcodePresss ? $qrcodePresss : 0;
            $qrcodePressCard = $qrcodePressCards ? $qrcodePressCards : 0;

            $workLinkStatistic[] = ["kfid"=>$kfid,"phase"=>$yesterday,"web"=>$pageView,"click"=>$workClick,"kefu"=>$linkWelcome,"qrcode_show"=>$qrcodeShow,"qrcode_press"=>$qrcodePress,"qrcode_card"=>$qrcodePressCards,"created_at"=>time()];

            // 更新剩余访问量
            Worklink::updateWorkLinkLimitPv($kfid,$pageView);
            // 更新累计统计
            Worklinktotal::updateWorkLinkTotal($kfid,$pageView,$workClick,$linkWelcome,$qrcodeShow,$qrcodePress,$qrcodePressCard);

            $handleNumber += 1;
            if ($handleNumber > 100) {
                Worklinkstatistic::creatWorkLinkStatistic($workLinkStatistic);
                $handleNumber = 1;
                $workLinkStatistic = [];
            }
        }

        // 再次保存遗漏的mysql
        if (!empty($workLinkStatistic)) {
            Worklinkstatistic::creatWorkLinkStatistic($workLinkStatistic);
        }

        // 删除3天以前的数据
        Redis::del('work_page_view_'.$beforeyesterday);
        Redis::del('work_click_'.$beforeyesterday);
        Redis::del('work_kefu_'.$beforeyesterday);
        Redis::del('work_qrcode_show_'.$beforeyesterday);
        Redis::del('work_qrcode_press_'.$beforeyesterday);
        Redis::del('work_qrcode_press_card_'.$beforeyesterday);
        return "success";
    }
}
